#include <stdio.h>
#include <string>

struct std_string
{
	union 
	{
		char buf[16];
		char * ptr;
	}u;

	size_t size;
	size_t capacity;
};

void dump_std_string(std::string s)
{
	struct std_string *p = (struct std_string *) &s;
	printf("[%s] size:%ld capacity:%ld\n", p->size>>16 ? p->u.ptr:p->u.buf, p->size, p->capacity);
}

int main()
{
	std::string s1 = "short string";
	std::string s2 = "string longer that 16 bytes";

	dump_std_string(s1);
	dump_std_string(s2);

	// that works without using c_str()
	printf("%s\n", &s1);
	printf("%s\n", &s2);
}

#if 0
/*
 * intel
 */
0000000000001249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>:
    1249:	f3 0f 1e fa          	endbr64 
    124d:	55                   	push   %rbp
    124e:	48 89 e5             	mov    %rsp,%rbp
    1251:	48 83 ec 20          	sub    $0x20,%rsp
    1255:	48 89 7d e8          	mov    %rdi,-0x18(%rbp)
    1259:	48 8b 45 e8          	mov    -0x18(%rbp),%rax
    125d:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1261:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1265:	48 8b 48 18          	mov    0x18(%rax),%rcx
    1269:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    126d:	48 8b 50 10          	mov    0x10(%rax),%rdx
    1271:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1275:	48 8b 40 10          	mov    0x10(%rax),%rax
    1279:	48 c1 e8 10          	shr    $0x10,%rax
    127d:	48 85 c0             	test   %rax,%rax
    1280:	74 09                	je     128b <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x42>
    1282:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1286:	48 8b 00             	mov    (%rax),%rax
    1289:	eb 04                	jmp    128f <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x46>
    128b:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    128f:	48 89 c6             	mov    %rax,%rsi
    1292:	48 8d 3d 6c 0d 00 00 	lea    0xd6c(%rip),%rdi        # 2005 <_ZStL19piecewise_construct+0x1>
    1299:	b8 00 00 00 00       	mov    $0x0,%eax
    129e:	e8 2d fe ff ff       	callq  10d0 <printf@plt>
    12a3:	90                   	nop
    12a4:	c9                   	leaveq 
    12a5:	c3                   	retq   

00000000000012a6 <main>:
    12a6:	f3 0f 1e fa          	endbr64 
    12aa:	55                   	push   %rbp
    12ab:	48 89 e5             	mov    %rsp,%rbp
    12ae:	53                   	push   %rbx
    12af:	48 81 ec 88 00 00 00 	sub    $0x88,%rsp
    12b6:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    12bd:	00 00 
    12bf:	48 89 45 e8          	mov    %rax,-0x18(%rbp)
    12c3:	31 c0                	xor    %eax,%eax
    12c5:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    12cc:	48 89 c7             	mov    %rax,%rdi
    12cf:	e8 7c fe ff ff       	callq  1150 <_ZNSaIcEC1Ev@plt>
    12d4:	48 8d 95 7f ff ff ff 	lea    -0x81(%rbp),%rdx
    12db:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    12df:	48 8d 35 3b 0d 00 00 	lea    0xd3b(%rip),%rsi        # 2021 <_ZStL19piecewise_construct+0x1d>
    12e6:	48 89 c7             	mov    %rax,%rdi
    12e9:	e8 32 fe ff ff       	callq  1120 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
    12ee:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    12f5:	48 89 c7             	mov    %rax,%rdi
    12f8:	e8 03 fe ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    12fd:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    1304:	48 89 c7             	mov    %rax,%rdi
    1307:	e8 44 fe ff ff       	callq  1150 <_ZNSaIcEC1Ev@plt>
    130c:	48 8d 95 7f ff ff ff 	lea    -0x81(%rbp),%rdx
    1313:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    1317:	48 8d 35 10 0d 00 00 	lea    0xd10(%rip),%rsi        # 202e <_ZStL19piecewise_construct+0x2a>
    131e:	48 89 c7             	mov    %rax,%rdi
    1321:	e8 fa fd ff ff       	callq  1120 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
    1326:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    132d:	48 89 c7             	mov    %rax,%rdi
    1330:	e8 cb fd ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    1335:	48 8d 55 80          	lea    -0x80(%rbp),%rdx
    1339:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    133d:	48 89 d6             	mov    %rdx,%rsi
    1340:	48 89 c7             	mov    %rax,%rdi
    1343:	e8 98 fd ff ff       	callq  10e0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
    1348:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    134c:	48 89 c7             	mov    %rax,%rdi
    134f:	e8 f5 fe ff ff       	callq  1249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
    1354:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1358:	48 89 c7             	mov    %rax,%rdi
    135b:	e8 90 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1360:	48 8d 55 a0          	lea    -0x60(%rbp),%rdx
    1364:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1368:	48 89 d6             	mov    %rdx,%rsi
    136b:	48 89 c7             	mov    %rax,%rdi
    136e:	e8 6d fd ff ff       	callq  10e0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
    1373:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1377:	48 89 c7             	mov    %rax,%rdi
    137a:	e8 ca fe ff ff       	callq  1249 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
    137f:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1383:	48 89 c7             	mov    %rax,%rdi
    1386:	e8 65 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    138b:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    138f:	48 89 c7             	mov    %rax,%rdi
    1392:	e8 99 fd ff ff       	callq  1130 <puts@plt>
    1397:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    139b:	48 89 c7             	mov    %rax,%rdi
    139e:	e8 8d fd ff ff       	callq  1130 <puts@plt>
    13a3:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    13a7:	48 89 c7             	mov    %rax,%rdi
    13aa:	e8 41 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    13af:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    13b3:	48 89 c7             	mov    %rax,%rdi
    13b6:	e8 35 fd ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    13bb:	b8 00 00 00 00       	mov    $0x0,%eax
    13c0:	48 8b 4d e8          	mov    -0x18(%rbp),%rcx
    13c4:	64 48 33 0c 25 28 00 	xor    %fs:0x28,%rcx
    13cb:	00 00 
    13cd:	0f 84 97 00 00 00    	je     146a <main+0x1c4>
    13d3:	e9 8d 00 00 00       	jmpq   1465 <main+0x1bf>
    13d8:	f3 0f 1e fa          	endbr64 
    13dc:	48 89 c3             	mov    %rax,%rbx
    13df:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    13e6:	48 89 c7             	mov    %rax,%rdi
    13e9:	e8 12 fd ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    13ee:	48 89 d8             	mov    %rbx,%rax
    13f1:	48 89 c7             	mov    %rax,%rdi
    13f4:	e8 47 fd ff ff       	callq  1140 <_Unwind_Resume@plt>
    13f9:	f3 0f 1e fa          	endbr64 
    13fd:	48 89 c3             	mov    %rax,%rbx
    1400:	48 8d 85 7f ff ff ff 	lea    -0x81(%rbp),%rax
    1407:	48 89 c7             	mov    %rax,%rdi
    140a:	e8 f1 fc ff ff       	callq  1100 <_ZNSaIcED1Ev@plt>
    140f:	eb 3d                	jmp    144e <main+0x1a8>
    1411:	f3 0f 1e fa          	endbr64 
    1415:	48 89 c3             	mov    %rax,%rbx
    1418:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    141c:	48 89 c7             	mov    %rax,%rdi
    141f:	e8 cc fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1424:	eb 1c                	jmp    1442 <main+0x19c>
    1426:	f3 0f 1e fa          	endbr64 
    142a:	48 89 c3             	mov    %rax,%rbx
    142d:	48 8d 45 c0          	lea    -0x40(%rbp),%rax
    1431:	48 89 c7             	mov    %rax,%rdi
    1434:	e8 b7 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    1439:	eb 07                	jmp    1442 <main+0x19c>
    143b:	f3 0f 1e fa          	endbr64 
    143f:	48 89 c3             	mov    %rax,%rbx
    1442:	48 8d 45 a0          	lea    -0x60(%rbp),%rax
    1446:	48 89 c7             	mov    %rax,%rdi
    1449:	e8 a2 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    144e:	48 8d 45 80          	lea    -0x80(%rbp),%rax
    1452:	48 89 c7             	mov    %rax,%rdi
    1455:	e8 96 fc ff ff       	callq  10f0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
    145a:	48 89 d8             	mov    %rbx,%rax
    145d:	48 89 c7             	mov    %rax,%rdi
    1460:	e8 db fc ff ff       	callq  1140 <_Unwind_Resume@plt>
    1465:	e8 a6 fc ff ff       	callq  1110 <__stack_chk_fail@plt>
    146a:	48 81 c4 88 00 00 00 	add    $0x88,%rsp
    1471:	5b                   	pop    %rbx
    1472:	5d                   	pop    %rbp
    1473:	c3                   	retq   
    1474:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
    147b:	00 00 00 
    147e:	66 90                	xchg   %ax,%ax

/*
 * arm
 */
00000000004009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>:
  4009ec:	a9bd7bfd 	stp	x29, x30, [sp, #-48]!
  4009f0:	910003fd 	mov	x29, sp
  4009f4:	f9000fa0 	str	x0, [x29, #24]
  4009f8:	f9400fa0 	ldr	x0, [x29, #24]
  4009fc:	f90017a0 	str	x0, [x29, #40]
  400a00:	f94017a0 	ldr	x0, [x29, #40]
  400a04:	f9400800 	ldr	x0, [x0, #16]
  400a08:	d350fc00 	lsr	x0, x0, #16
  400a0c:	f100001f 	cmp	x0, #0x0
  400a10:	54000080 	b.eq	400a20 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x34>  // b.none
  400a14:	f94017a0 	ldr	x0, [x29, #40]
  400a18:	f9400000 	ldr	x0, [x0]
  400a1c:	14000002 	b	400a24 <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x38>
  400a20:	f94017a0 	ldr	x0, [x29, #40]
  400a24:	f94017a1 	ldr	x1, [x29, #40]
  400a28:	f9400822 	ldr	x2, [x1, #16]
  400a2c:	f94017a1 	ldr	x1, [x29, #40]
  400a30:	f9400c23 	ldr	x3, [x1, #24]
  400a34:	90000001 	adrp	x1, 400000 <_init-0x7f8>
  400a38:	9130c024 	add	x4, x1, #0xc30
  400a3c:	aa0003e1 	mov	x1, x0
  400a40:	aa0403e0 	mov	x0, x4
  400a44:	97ffffa3 	bl	4008d0 <printf@plt>
  400a48:	d503201f 	nop
  400a4c:	a8c37bfd 	ldp	x29, x30, [sp], #48
  400a50:	d65f03c0 	ret

0000000000400a54 <main>:
  400a54:	a9b57bfd 	stp	x29, x30, [sp, #-176]!
  400a58:	910003fd 	mov	x29, sp
  400a5c:	f9000bf3 	str	x19, [sp, #16]
  400a60:	910183a0 	add	x0, x29, #0x60
  400a64:	97ffff93 	bl	4008b0 <_ZNSaIcEC1Ev@plt>
  400a68:	910183a2 	add	x2, x29, #0x60
  400a6c:	90000000 	adrp	x0, 400000 <_init-0x7f8>
  400a70:	91314001 	add	x1, x0, #0xc50
  400a74:	910103a0 	add	x0, x29, #0x40
  400a78:	97ffff8a 	bl	4008a0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
  400a7c:	910183a0 	add	x0, x29, #0x60
  400a80:	97ffff7c 	bl	400870 <_ZNSaIcED1Ev@plt>
  400a84:	9101a3a0 	add	x0, x29, #0x68
  400a88:	97ffff8a 	bl	4008b0 <_ZNSaIcEC1Ev@plt>
  400a8c:	9101a3a2 	add	x2, x29, #0x68
  400a90:	90000000 	adrp	x0, 400000 <_init-0x7f8>
  400a94:	91318001 	add	x1, x0, #0xc60
  400a98:	910083a0 	add	x0, x29, #0x20
  400a9c:	97ffff81 	bl	4008a0 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_@plt>
  400aa0:	9101a3a0 	add	x0, x29, #0x68
  400aa4:	97ffff73 	bl	400870 <_ZNSaIcED1Ev@plt>
  400aa8:	910103a1 	add	x1, x29, #0x40
  400aac:	9101c3a0 	add	x0, x29, #0x70
  400ab0:	97ffff78 	bl	400890 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
  400ab4:	9101c3a0 	add	x0, x29, #0x70
  400ab8:	97ffffcd 	bl	4009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
  400abc:	9101c3a0 	add	x0, x29, #0x70
  400ac0:	97ffff64 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400ac4:	910083a1 	add	x1, x29, #0x20
  400ac8:	910243a0 	add	x0, x29, #0x90
  400acc:	97ffff71 	bl	400890 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_@plt>
  400ad0:	910243a0 	add	x0, x29, #0x90
  400ad4:	97ffffc6 	bl	4009ec <_Z15dump_std_stringNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
  400ad8:	910243a0 	add	x0, x29, #0x90
  400adc:	97ffff5d 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400ae0:	910103a0 	add	x0, x29, #0x40
  400ae4:	97ffff67 	bl	400880 <puts@plt>
  400ae8:	910083a0 	add	x0, x29, #0x20
  400aec:	97ffff65 	bl	400880 <puts@plt>
  400af0:	910083a0 	add	x0, x29, #0x20
  400af4:	97ffff57 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400af8:	910103a0 	add	x0, x29, #0x40
  400afc:	97ffff55 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b00:	52800000 	mov	w0, #0x0                   	// #0
  400b04:	14000019 	b	400b68 <main+0x114>
  400b08:	aa0003f3 	mov	x19, x0
  400b0c:	910183a0 	add	x0, x29, #0x60
  400b10:	97ffff58 	bl	400870 <_ZNSaIcED1Ev@plt>
  400b14:	aa1303e0 	mov	x0, x19
  400b18:	97ffff72 	bl	4008e0 <_Unwind_Resume@plt>
  400b1c:	aa0003f3 	mov	x19, x0
  400b20:	9101a3a0 	add	x0, x29, #0x68
  400b24:	97ffff53 	bl	400870 <_ZNSaIcED1Ev@plt>
  400b28:	1400000c 	b	400b58 <main+0x104>
  400b2c:	aa0003f3 	mov	x19, x0
  400b30:	9101c3a0 	add	x0, x29, #0x70
  400b34:	97ffff47 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b38:	14000006 	b	400b50 <main+0xfc>
  400b3c:	aa0003f3 	mov	x19, x0
  400b40:	910243a0 	add	x0, x29, #0x90
  400b44:	97ffff43 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b48:	14000002 	b	400b50 <main+0xfc>
  400b4c:	aa0003f3 	mov	x19, x0
  400b50:	910083a0 	add	x0, x29, #0x20
  400b54:	97ffff3f 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b58:	910103a0 	add	x0, x29, #0x40
  400b5c:	97ffff3d 	bl	400850 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>
  400b60:	aa1303e0 	mov	x0, x19
  400b64:	97ffff5f 	bl	4008e0 <_Unwind_Resume@plt>
  400b68:	f9400bf3 	ldr	x19, [sp, #16]
  400b6c:	a8cb7bfd 	ldp	x29, x30, [sp], #176
  400b70:	d65f03c0 	ret
  400b74:	00000000 	.inst	0x00000000 ; undefined


#endif

